package model.dao.registerOrder;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


import db.dbconnection;
import log.MyLogger;
import model.service.Employee;
import model.service.ServiceOrder;
import model.dao.registerOrder.SoStatusTypeDAOImpl;


public class ServiceOrderImpl {

	String sql = "" ;
	db.dbconnection dbconnection ;
	java.sql.Connection myConnection ;
	Statement st ;	
	ResultSet rs;
	
	/**
	 * @param serviceOrder
	 * @return inserted service order id
	 */
	public int insertServiceOrder(ServiceOrder serviceOrder) {
		int serviceOrderId = 0;
		try {
		    dbconnection = new dbconnection();
		    myConnection = dbconnection.getConnection();
		    st = myConnection.createStatement();
		    
			String sql = "SELECT f_insert_service_order("+serviceOrder.getCreatedBy().getEmployee()+", " +
					""+serviceOrder.getSoStatusType().getSoStatusType()+", " +
					""+serviceOrder.getServiceRequest().getRequest()+", '"+serviceOrder.getNote()+"')";
		    
			rs=st.executeQuery(sql);
		    rs.next();
		    serviceOrderId=rs.getInt(1);
		    this.dbconnection.close();
		} catch (Exception e) {
			e.printStackTrace();
//			MyLogger.Log("ServiceOrderDAOImpl.insertServiceOrder()", e.getMessage());
		}
		return serviceOrderId;
	}
	
	public boolean updateServiceOrder(ServiceOrder so) {
		try {
		    dbconnection = new dbconnection();
		    myConnection = dbconnection.getConnection();
		    st = myConnection.createStatement();
			String sql = "UPDATE service_order SET so_status_type_fk="+so.getSoStatusType().getSoStatusType()+" WHERE service_order="+so.getServiceOrder();
			st.executeUpdate(sql);
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceOrderDAOImpl.updateServiceOrder()", e.getMessage());
			return false;
		}
		return true;
	}
	
	public ServiceOrder getServiceOrderById(int serviceOrderId) {
		if(serviceOrderId < 1){
			return null;
		}
		
		boolean ok = false;
		ServiceOrder serviceOrder = new ServiceOrder();
		serviceOrder.setServiceOrder(serviceOrderId);
		try {
		    dbconnection = new dbconnection();
		    myConnection = dbconnection.getConnection();
		    st = myConnection.createStatement();
			String sql = "SELECT * FROM service_order WHERE service_order = "+ serviceOrderId;
			ResultSet r = st.executeQuery(sql);
			
			
			
			EmployeeDAOImpl employeeDAOImpl = new EmployeeDAOImpl();
			ServiceRequestDAOImpl serviceRequestDAOImpl = new ServiceRequestDAOImpl();
			ServiceActionDAOImpl serviceActionDAOImpl = new ServiceActionDAOImpl();
			ServicePartDAOImpl servicePartDAOImpl = new ServicePartDAOImpl();
			ServiceDeviceDAOImpl serviceDeviceDAOImpl = new ServiceDeviceDAOImpl();
			SoStatusTypeDAOImpl soStatusTypeDAOImpl = new SoStatusTypeDAOImpl();
			
			
			
			while(r.next()){
				serviceOrder.setCreated(r.getTimestamp("created"));
				serviceOrder.setCreatedBy(new Employee(r.getInt("created_by"), employeeDAOImpl.getPersonByEmployeeId(r.getInt("created_by"))));
				serviceOrder.setNote(r.getString("note"));
				serviceOrder.setSoStatusType(soStatusTypeDAOImpl.getStatusTypeById(r.getInt("so_status_type_fk")));
				serviceOrder.setServiceRequest(serviceRequestDAOImpl.getServiceRequestById(r.getInt("service_request_fk")));
				serviceOrder.setUpdatedBy(new Employee(r.getInt("updated_by"), employeeDAOImpl.getPersonByEmployeeId(r.getInt("updated_by"))));
				serviceOrder.setUpdated(r.getTimestamp("updated"));
				serviceOrder.setStatusChanged(r.getTimestamp("status_changed"));
				serviceOrder.setStatusChangedBy(new Employee(r.getInt("status_changed_by"), employeeDAOImpl.getPersonByEmployeeId(r.getInt("status_changed_by"))));
				serviceOrder.setPriceTotal(r.getFloat("price_total"));
				serviceOrder.setServiceActions(serviceActionDAOImpl.getServiceActions(serviceOrderId));
				serviceOrder.setServiceParts(servicePartDAOImpl.getServiceParts(serviceOrderId));
				serviceOrder.setServiceDevices(serviceDeviceDAOImpl.getServiceDevicesByServiceOrder(serviceOrderId));
				ok = true;	
			}
			this.dbconnection.close();
		} catch (Exception e) {
//			MyLogger.Log("ServiceOrderDAOImpl.getServiceOrderById()", e.getMessage());
			ok = false;
		}
		
		if(ok){
			return serviceOrder;
		}
		else{
			return null;
		}
	}
	
}
